package com.hyp.pay.UI.Home.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.hyp.pay.R;
import com.hyp.pay.utils.SPUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeActivity extends Activity {

    private ArrayList<Integer> datas = new ArrayList<>();

    @BindView(R.id.weclomeImage)
    ImageView mWeclomeImage;
    @BindView(R.id.banner)
    ConvenientBanner mBanner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
//        mBanner.setCanLoop(true);
        switchWelcomePage();
    }

    // 选择展示启动页或闪屏
    private void switchWelcomePage() {
        if (SPUtil.getShowPage()) {
            mWeclomeImage.setVisibility(View.GONE);
            mBanner.setVisibility(View.VISIBLE);
            showPage();
        } else {
            mWeclomeImage.setVisibility(View.VISIBLE);
            mBanner.setVisibility(View.GONE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    doJump();
                }
            }, 1000);
        }
    }

    private void showPage() {
        mBanner.setCanLoop(true);
        datas.add(R.mipmap.welcom1);
        datas.add(R.mipmap.welcome2);
        datas.add(R.mipmap.welcome3);
        mBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, datas)
                .setPointViewVisible(true)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        if (position == 2) {
                            SPUtil.setShowPage(false);
                            doJump();
                        }
                    }
                })
//                .setPageIndicator(new int[]{R.mipmap.spit1, R.mipmap.spit2})
                .setManualPageable(true);
        mBanner.setCanLoop(false);

    }

    private void doJump() {

            startActivity(new Intent(this, HomeActivity.class));
            finish();
    }


    public class NetworkImageHolderView implements Holder<Integer> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, Integer data) {
            imageView.setImageResource(data);
        }
    }
//
//    @OnClick(R.id.tiaoguo)
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.tiaoguo:
//                SPUtil.setShowPage(false);
//                startActivity(new Intent(this, LoginActivity.class));
//                finish();
//                break;
//        }
//    }
}
